package com.google.screen;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBAdapter {

	private static final int DATABASE_VERSION = 1;
	private final Context mCtx;
	private DatabaseHelper mDBHelper;
	public static SQLiteDatabase mDB;
	private static final String DATABASE_NAME = "dbhistoryscancode";
	private ContentValues content;
	private static final String CREATE_HISTORY_TABLE = "CREATE TABLE historyscancode (barcode text, coupon_id text, " +
			"category_id text, title text, description text, image text, nbr_to_print text, nbr_printed text, start_date text, " +
			"end_date text, status text, checkout text)";
	private static final String CREATE_FAQS_TABLE = "CREATE TABLE faqs (question text, answer text)";
	/*private static final String INSERT_HISTORY_1 = "INSERT INTO historyscancode VALUES ('1263360744', 6, 1, " +
			"'2 achet\u00e9s un offert 33%', " +
			"'Un petit dejeuner enrichi par du lait et des fruits, un jus pur et frais \u00e0 base du lait.', " +
			"'cb7b2de1d1374aea148c0362bc3e280a596c964e.jpg', 15, 0, '15/06/2013', '30/06/2013', 1, 0);";*/

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_HISTORY_TABLE);
			db.execSQL(CREATE_FAQS_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			onCreate(db);
		}
	}

	public DBAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public DBAdapter open() {
		mDBHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDB = mDBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDBHelper.close();
	}

	public void insertHistory(ContentValues content) {
		mDB.insert("historyscancode", null, content);
	}

	public ArrayList<ContentValues> queryAllHistory() {
		ArrayList<ContentValues> alstAllHistory = new ArrayList<ContentValues>();
		Cursor cur = mDB.query("historyscancode", null, null, null, null, null, null);
		cur.moveToFirst();
		while (cur.isAfterLast() == false) {
			content = new ContentValues();
			getAllHistory(cur);
			alstAllHistory.add(content);
			cur.moveToNext();
		}
		cur.close();
		return alstAllHistory;
	}
	
	private void getAllHistory(Cursor cur) {
		content.put("barcode",
				cur.getString(cur.getColumnIndexOrThrow("barcode")));
		content.put("coupon_id",
				cur.getString(cur.getColumnIndexOrThrow("coupon_id")));
		content.put("category_id",
				cur.getString(cur.getColumnIndexOrThrow("category_id")));
		content.put("title",
				cur.getString(cur.getColumnIndexOrThrow("title")));
		content.put("description",
				cur.getString(cur.getColumnIndexOrThrow("description")));
		content.put("image",
				cur.getString(cur.getColumnIndexOrThrow("image")));
		content.put("nbr_to_print",
				cur.getString(cur.getColumnIndexOrThrow("nbr_to_print")));
		content.put("nbr_printed",
				cur.getString(cur.getColumnIndexOrThrow("nbr_printed")));
		content.put("start_date",
				cur.getString(cur.getColumnIndexOrThrow("start_date")));
		content.put("end_date",
				cur.getString(cur.getColumnIndexOrThrow("end_date")));
		content.put("status",
				cur.getString(cur.getColumnIndexOrThrow("status")));
		content.put("checkout",
				cur.getString(cur.getColumnIndexOrThrow("checkout")));
	}
}
